<?
$r = "../";
/*
----------------------------------------
-----    Page logic description    -----
----------------------------------------
INPUT:
	mid		=> monitor id
	type	=> CONTENT_TYPE_* [16:32] (1 - RSS, 2 - ATOM, etc.)
	guid	=> monitor's guid

OUTPUT:
	RSS and ATOM feed output

LINK:
	/members/feed.php?mid=XXX&format=ZZZ&guid=YYY

OLD INPUT: (depricated) 
	link	=> /members/feed.php?id=2&guid=ea95ece5d170e70d06478f1ebf6e2def
	id		=> feed id
	guid	=> monitor's guid

DEBUG:
http://server/sites/w3m/members/feed.php?mid=2&type=1&guid=ea95ece5d170e70d06478f1ebf6e2def
*/

require_once($r."_engine/header_nocache.php");
require_once($r."_engine/c_feed.php");
require_once($r."_engine/c_sql_table.php");
require_once($r."_engine/c_monitor.php");
require_once($r."_engine/c_account.php");
require_once($r."_engine/c_sp_gate.php");


/* Load monitor - first of all */
$m = new CMonitor();
$r = $m->LoadByQuery($q =
	"SELECT * FROM monitors ".
	"WHERE id='".$mid."' AND activation_code='$guid';"
);
if ($r !== true) {
	/* monitor not found or wrong guid => page not found */
	@header("HTTP/1.0 404 Not Found");
	die();
}


/* Load feed from DB*/
for ($counter = 0; true; $counter++) 
{
	$feeds = execute_query($q = 
		"SELECT * FROM feeds ".
		"WHERE monitor_id='".$mid."' AND type='".(((int)$type << 16)| CONTENT_TYPE_XML)."' ".
		"LIMIT 1"
	);

	if (@mysql_num_rows($feeds) > 0)
	{
		$feed = @mysql_fetch_array($feeds);
		@mysql_free_result($feeds);
	
		print_feed($feed["feed"]);
		die();
	}
	
	if ($counter > 0) {
		@header("HTTP/1.0 500 Internal Server Error");
		die();
	}
	
	/* Feed not found in DB => call service to create the last one */
	global $g_spg;
	$res = $g_spg->RequestFeed($mid, $type);
}


/* Show default feed - DEPRICATED */
/*
$a = new CAccount();
$a->LoadByID($m->data["account_id"]);


$rss_template = '
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>W3Monitor: [%title%]</title>
    <link>http://w3monitor.com/members/cp_monitor_history.php?monitor_id=[%monitor_id%]</link>
    <description>[%title%]</description>
    <lastBuildDate>[%download_time%]</lastBuildDate>
    <generator>W3Monitor. Advanced web monitoring made easy</generator>
    <item>
      <title>[%text_title%]</title>
      <link>http://w3monitor.com/members/cp_monitor_history.php?monitor_id=[%monitor_id%]</link>
      <description><![CDATA[[%text_body%]]]></description>
      <pubDate>[%download_time%]</pubDate>
    </item>
  </channel>
</rss>
';

$atom_template = '
<?xml version="1.0" encoding="UTF-8"?>
<feed version="0.3" xml:lang="en" xmlns="http://purl.org/atom/ns#">
    <generator>W3Monitor. Advanced web monitoring made easy</generator>
    <title>W3Monitor: [%title%]</title>
    <link rel="alternate" type="text/html" href="http://w3monitor.com/members/cp_monitor_history.php?monitor_id=[%monitor_id%]"/>
    <author>
        <name>W3Monitor</name>
        <email>w3monitor@w3monitor.com</email>
    </author>
    <copyright>&amp;copy;2006 W3Monitor</copyright>
    <modified>[%download_time%]</modified>
    <entry>
        <title>[%text_title%]</title>
        <link rel="alternate" type="text/html" href="http://w3monitor.com/members/cp_monitor_history.php?monitor_id=[%monitor_id%]"/>
        <issued>[%download_time%]</issued>
        <modified>[%download_time%]</modified>
        <content type="text/html" mode="escaped">[%text_body%]</content>
    </entry>
</feed>
';


$format = CONTENT_TYPE_RSS; 

$s = ""; $s_f = "Unknown";
if ($format == CONTENT_TYPE_RSS) { 
	$s = $rss_template; 
	$s_f = "RSS"; 
}
if ($format == CONTENT_TYPE_ATOM) {
	$s = $atom_template;
	$s_f = "ATOM"; 
}


$s_tb = 
$a->data["first_name"].' '.$a->data["last_name"].', you are successfully '.
'subscribed to '.$s_f.' feed of '.$m->data["url"].' page '.
'via W3Monitor service. As no changes were detected since your subscription, '.
'currently this feed is empty. Items will be added upon detection of changes at '.
'the site being monitored.';

$s_dt = date("D, j M Y H:i:s \G\M\T", $m->data["created"]);

$s = str_replace("[%title%]", 
	$m->data["title"], $s);
$s = str_replace("[%download_time%]", 
	$s_dt, $s);
$s = str_replace("[%monitor_id%]", 
	$m->id, $s);
$s = str_replace("[%text_title%]", 
	"Feed output is ready", $s);
$s = str_replace("[%text_body%]", 
	$s_tb, $s);

print_feed($s);
*/
?>